﻿using System;
using System.Collections.Generic;
using System.Text;
using Cashier_Model;
using System.Data.Common;
using Cashier_DAO.db;
using Cashier_DAO;
using System.Data;

namespace Cashier_Service
{
    public class StoreInfoService
    {
        private StoreInfoDAO storeInfoDAO = new StoreInfoDAO();

        /// <summary>
        /// 查询门店信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="storeNo"></param>
        /// <returns></returns>
        public StoreInfo GetStoreInfo(Int64 id, String storeNo)
        {
            StoreInfo storeInfo = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    storeInfo = new StoreInfo();
                    storeInfo.Id = id;
                    storeInfo.StoreNo = storeNo;
                    storeInfo = storeInfoDAO.GetStoreInfo(conn, storeInfo);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return storeInfo;
        }

        /// <summary>
        /// 查询门店信息
        /// </summary>
        /// <param name="storeInfo"></param>
        /// <returns></returns>
        public DataTable GetStoreInfoTable(StoreInfo storeInfo)
        {
            DataTable dt = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    dt = storeInfoDAO.GetStoreInfoTable(conn, storeInfo);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 查询门店记录数
        /// </summary>
        /// <param name="storeInfo"></param>
        /// <returns></returns>
        public long GetStoreInfoRecord(StoreInfo storeInfo)
        {
            long record = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    record = storeInfoDAO.GetStoreInfoRecord(conn, storeInfo);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return record;
        }

        /// <summary>
        /// 新增门店信息
        /// </summary>
        /// <param name="storeInfo"></param>
        /// <returns></returns>
        public long InsertStoreInfo(StoreInfo storeInfo)
        {
            long id = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    id = storeInfoDAO.InsertStoreInfo(conn, storeInfo);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return id;
        }

        /// <summary>
        /// 更新门店信息
        /// </summary>
        /// <param name="storeInfo"></param>
        /// <returns></returns>
        public int UpdateStoreInfo(StoreInfo storeInfo)
        {
            int flag = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    flag = storeInfoDAO.UpdateStoreInfo(conn, storeInfo);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        /// <summary>
        /// 删除门店信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DeleteStoreInfo(String ids)
        {
            int count = 0;
            try
            {
                if (!String.IsNullOrEmpty(ids))
                {
                    using (DbConnection conn = ConnectionFactory.GetDbConnection())
                    {
                        conn.Open();
                        StoreInfo storeInfo = new StoreInfo();
                        String[] idArr = ids.Split(new Char[] { ',' });
                        foreach (String lopid in idArr)
                        {
                            storeInfo.Id = Int64.Parse(lopid);
                            count += storeInfoDAO.DeleteStoreInfo(conn, storeInfo);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return count;
        }
    }
}
